package model

// Category 栏目模型
type Category struct {
	ID    uint   `gorm:"primary_key;not null;auto_increment" json:"id"`
	Title string `gorm:"type:varchar(32);unique_index;not null" json:"title" binding:"required"`
}

// GetCategory 获取所有栏目
func GetCategory() (categories []Category, err error) {
	err = DB.Find(&categories).Error

	return
}

// AddCategory 添加栏目
func (cate *Category) AddCategory() (err error) {
	err = DB.Create(&cate).Error
	return
}

// DeleteCategory 删除栏目
func DeleteCategory(id uint) (err error) {
	err = DB.Delete(&Article{}, "category_id=?", id).Error
	if err == nil {
		err = DB.Delete(&Category{}, id).Error
	}
	return
}
